<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>部门管理</title>
    <style>
        .newtitle {
            PADDING-LEFT: 15px;
            BACKGROUND: url('');
            PADDING-BOTTOM: 4px;
            BORDER-LEFT: #c0dcf2 1px solid;
        }
    </style>
    <jsp:include page="easyuiIncludeJS.jsp"/>
    <script type="text/javascript">
        $(function () {
            var pager = $(".easyui-datagrid").datagrid('getPager');
            $(pager).pagination({
                total:${pager.totalRows},
                pageSize:${pager.pageRow},
                displayMsg: '{from}-{to}/{total}',
                pageNumber:${pager.cur_page},
                afterPageText: '页，共{pages}页',
                pageList: [5, 10, 20, 30, 50, 100],
                loading: false,
                showPageList: true,
                showRefresh: true,
                //刷新方法
                onBeforeRefresh: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/dep/list?cur_page=" + p + "&pageRow=" + s;
                },
                onSelectPage: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/dep/list?cur_page=" + p + "&pageRow=" + s;
                }
            });
        });

        function del() {
            $.post(
                "${pageContext.request.contextPath }/admin/dep/isDepParentId",
                {
                    "parentId": $("#depid").val()
                }, function (data) {
                    if (data == "") {
                        $.post(
                            "${pageContext.request.contextPath }/admin/dep/isEmpDepId",
                            {
                                "depid": $("#depid").val()
                            }, function (data) {
                                if (data == "") {
                                    window.location.href = "${pageContext.request.contextPath}/admin/dep/del?depid=" + $("#depid").val();
                                    return;
                                } else {
                                    alert("不能删除该部门");
                                    return;
                                }

                            }, "json"
                        );
                    } else {
                        alert("不能删除该部门");
                        return;
                    }

                }, "json"
            );
        }

        function edit(obj) {
            var depid = obj.id;
            var url = "${pageContext.request.contextPath}/admin/dep/json";
            $.post(
                url,
                {
                    "depid": depid
                },
                updemp,
                "json"
            );
        }

        function updemp(data) {
            $("#depid").val(data.depid);
            $("#personnel").val(data.personnel);
            $("#depName").val(data.depName);
            $("#parentId").val(data.parentId);
            $("#chairman").val(data.chairman);
            $("#remark").val(data.remark);
        }

        function add() {
            $("#depid").val('0');
            $("#personnel").val('');
            $("#depName").val('');
            $("#parentId").val('');
            $("#chairman").val('');
            $("#remark").val('');
        }

        function save() {
            if ($("#depName").val() == "") {
                alert("请输入部门名称");
                $("#depName").focus();
                return false;
            }

            if ($("#parentId").val() == "") {
                alert("请输入父部门名称");
                $("#parentId").focus();
                return false;
            }

            var url = "${pageContext.request.contextPath}/admin/dep/add";
            var depid = $("#depid").val();
            var depName = $("#depName").val();
            var parentId = $("#parentId").val();
            var chairman = $("#chairman").val();
           var personnel=$("#personnel").val();
            var remark = $("#remark").val();
            $.post(
                url,

                {
                    "depid": depid,
                    "depName": depName,
                    "parentId": parentId,
                    "chairman": chairman,
                    "remark": remark,
                    "personnel": personnel
                },
                function (data) {
                    window.location.href = "${pageContext.request.contextPath }/admin/dep/list";
                }
            )
        }


    </script>
</head>
<body>
<div class="easyui-panel" style="padding:5px;height: 200px;">
    <ul id="tt1" class="easyui-tree" data-options="animate:true,dnd:true" style="height:250px">
        <li data-options="state:'open'">
            <c:forEach items="${pager.datas}" var="d">
                <c:if test="${d.dep==null}">
                    <span><a href="javascript:void(0)" onclick="edit(this)" id="${d.depid}">${d.depName}</a></span>
                    <c:forEach items="${pager.datas}" var="d2">
                        <c:if test="${d.depid==d2.parentId}">
                            <ul>
                                <li data-options="state:'open'">
                                    <span><a href="javascript:void(0)" onclick="edit(this)"
                                             id="${d2.depid}">${d2.depName}</a></span>
                                    <c:forEach items="${pager.datas}" var="d3">
                                        <c:if test="${d2.depid==d3.parentId}">
                                            <ul>
                                                <li data-options="state:'open'">
                                                    <span><a href="javascript:void(0)" onclick="edit(this)"
                                                             id="${d3.depid}">${d3.depName}</a></span>
                                                    <c:forEach items="${pager.datas}" var="d4">
                                                        <c:if test="${d3.depid==d4.parentId}">
                                                            <ul>
                                                                <li data-options="state:'open'">
                                                                    <span><a href="javascript:void(0)"
                                                                             onclick="edit(this)"
                                                                             id="${d4.depid}">${d4.depName}</a></span>
                                                                    <c:forEach items="${pager.datas}" var="d5">
                                                                        <c:if test="${d4.depid==d5.parentId}">
                                                                            <ul>
                                                                                <li data-options="state:'open'">
                                                                                    <span><a href="javascript:void(0)"
                                                                                             onclick="edit(this)"
                                                                                             id="${d5.depid}">${d5.depName}</a></span>
                                                                                    <c:forEach items="${pager.datas}"
                                                                                               var="d6">
                                                                                        <c:if test="${d5.depid==d6.parentId}">
                                                                                            <ul>
                                                                                                <li data-options="state:'open'">
                                                                                                    <span><a
                                                                                                            href="javascript:void(0)"
                                                                                                            onclick="edit(this)"
                                                                                                            id="${d6.depid}">${d6.depName}</a></span>
                                                                                                </li>
                                                                                            </ul>
                                                                                        </c:if>
                                                                                    </c:forEach>
                                                                                </li>
                                                                            </ul>
                                                                        </c:if>
                                                                    </c:forEach>
                                                                </li>
                                                            </ul>
                                                        </c:if>
                                                    </c:forEach>
                                                </li>
                                            </ul>
                                        </c:if>
                                    </c:forEach>
                                </li>
                            </ul>
                        </c:if>
                    </c:forEach>
                </c:if>
            </c:forEach>
        </li>
    </ul>
</div>
<div id="tt" class="easyui-tabs" style="height:320px">
    <div title="部门管理" data-options="closable:false,cache:false" style="padding:0px;">
        <div class="newtitle">
            <th>
                <a class="easyui-linkbutton" plain="true" icon="icon-add" href="javascript:void(0);"
                   onclick="add();">新增</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-save" href="javascript:void(0);"
                   onclick="save();">保存</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-cut" href="javascript:void(0);"
                   onclick="del();">删除</a>
            </th>
        </div>
        <form name="form1" enctype="multipart/form-data" method="post">
            <input type="hidden" id="depid" name="depid" value="0"/>
            <table width="100%" align="center" >
                <tr style="height: 40px">
                    <td align="right">部门名称：</td>
                    <td>
                        <input id="depName" name="depName"
                               data-options="prompt:'请输入部门名称',validType:'text'" style="width:220px;">
                    </td>
                </tr>
                <tr style="height: 40px">
                    <td align="right">上级部门名称：</td>
                    <td>
                        <select name="parentId" id="parentId"
                                style="width:220px;">
                            <option value="0">-----最高部门-----</option>
                            <c:forEach items="${pager.datas}" var="d">
                                <option value="${d.depid}">${d.depName}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr style="height: 40px">
                    <td align="right">部门负责人：</td>
                    <td>
                        <select name="chairman" id="chairman"
                                style="width:220px;">
                            <c:forEach items="${empList}" var="e">
                                <option value="${e.empId}">${e.empName}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr style="height: 40px">
                    <td align="right">备注：</td>
                    <td colspan="5">
                        <textarea id="remark" name="remark"
                                              style="width:50%; height:80px;" ></textarea>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>
